import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';

/// 导航调用原生工具类
class NavigateNativeUtil {
  static const _methodChannelName = 'flutter.keqiang.com/navigation';

  static const _platformMethod = MethodChannel(_methodChannelName);

  /// 退出当前原生页面
  ///
  /// 注意：Android端的FlutterActivity 必须实现[IKqRoute]
  static void closeNativePage() async {
    try {
      await _platformMethod.invokeMethod('close');
    } on PlatformException catch (e) {
      debugPrint(e.message);
    }
  }

  /// 打开原生页面
  ///
  /// 注意：Android端的FlutterActivity 必须实现[IKqRoute]
  static void openNativePage(String pageName,
      {Map<String, dynamic>? params, Function(dynamic result)? callback}) {
    try {
      _platformMethod
          .invokeMethod('open', {'pageName': pageName, 'params': params}).then(
              (value) => (value) {
                    callback?.call(value);
                  });
    } on PlatformException catch (e) {
      debugPrint(e.message);
    }
  }

  /// 退出登录
  static void exitLogin() async {
    try {
      await _platformMethod.invokeMethod('exitLogin');
    } on PlatformException catch (e) {
      debugPrint(e.message);
    }
  }
}
